package com.cuitrip.business.home.travel.model;

import android.widget.ListView;

import com.alibaba.fastjson.annotation.JSONField;
import com.lab.adapter.IAdapterData;
import com.lab.utils.ArrayUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 过滤的item
 * Created by ak on 16/1/14.
 */
public class FilterInfo implements IAdapterData, PopupFilterCategory<FilterInfo.ValueInfo>, Serializable {
    public final static String GENDER_KEY = "gender";
    public final static String CATEGORY_KEY = "p_cate";
    public final static String LANGUAGE_KEY = "language";
    private static HashMap<String, FilterType> filterTypeMap = new HashMap<>();

    static {
        filterTypeMap.put(GENDER_KEY, FilterType.GENDER);
        filterTypeMap.put(CATEGORY_KEY, FilterType.CATEGORY);
        filterTypeMap.put(LANGUAGE_KEY, FilterType.LANGUAGE);
    }

    public String desc;
    public String searchKey;
    public ArrayList<ValueInfo> values;

    public static boolean containsType(String type) {
        return filterTypeMap.containsKey(type);
    }

    public static FilterType getFilterType(String type) {
        return filterTypeMap.get(type);
    }

    public int getChoiceMode() {
        if (GENDER_KEY.equals(searchKey)) {
            return ListView.CHOICE_MODE_SINGLE;
        } else {
            return ListView.CHOICE_MODE_MULTIPLE;
        }
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String getCategoryTitle() {
        return desc;
    }

    public String getSearchKey() {
        return searchKey;
    }

    public void setSearchKey(String searchKey) {
        this.searchKey = searchKey;
    }

    @Override
    public ArrayList<ValueInfo> getList() {
        return values;
    }

    public ArrayList<ValueInfo> getValues() {
        return values;
    }

    public void setValues(ArrayList<ValueInfo> values) {
        this.values = values;
    }

    public List<ValueInfo> getSelectedValueList() {
        List<ValueInfo> selectedValueList = new ArrayList<>();
        for (ValueInfo valueInfo : values) {
            if (valueInfo.isSelected()) {
                selectedValueList.add(valueInfo);
            }
        }
        return selectedValueList;
    }

    public List<ValueInfo> getShowValues(){
        List<ValueInfo> shownValueInfoList = new ArrayList<>();
        for (ValueInfo valueInfo : values) {
            if (valueInfo.isShow()) {
                shownValueInfoList.add(valueInfo);
            }
        }
        return shownValueInfoList;
    }


    public enum FilterType {


        GENDER("gender") {
            @Override
            public String getOutPutValue(FilterInfo filterInfo) {
                List<FilterInfo.ValueInfo> selectedValueList = filterInfo.getSelectedValueList();
                if (!ArrayUtil.isNullOrEmpty(selectedValueList)) {
                    return selectedValueList.get(0).getValue();
                }
                return "";
            }
        },
        CATEGORY("p_cate") {
            @Override
            public String getOutPutValue(FilterInfo filterInfo) {
                List<FilterInfo.ValueInfo> selectedValueList = filterInfo.getSelectedValueList();
                StringBuilder cateBuilder = new StringBuilder();
                for (FilterInfo.ValueInfo valueInfo : selectedValueList) {
                    if (cateBuilder.length() > 0) {
                        cateBuilder.append(",");
                    }
                    cateBuilder.append(valueInfo.getValue());
                }
                return cateBuilder.toString();
            }
        },
        LANGUAGE("language") {
            @Override
            public String getOutPutValue(FilterInfo filterInfo) {
                List<FilterInfo.ValueInfo> selectedValueList = filterInfo.getSelectedValueList();
                int code = 0;
                try {
                    for (FilterInfo.ValueInfo valueInfo : selectedValueList) {
                        code += Integer.parseInt(valueInfo.getValue());
                    }
                    if (code != 0) {
                        return String.valueOf(code);
                    }
                } catch (Exception e) {

                }
                return null;
            }
        };

        String key;

        FilterType(String key) {
            this.key = key;
        }

        public abstract String getOutPutValue(FilterInfo filterInfo);

    }

    public class ValueInfo implements PopupFilterItem, Serializable {
        @JSONField(name = "v")
        private String value;
        @JSONField(name = "k")
        private String key;
        private boolean isSelected;
        private boolean isShow = true;

        @Override
        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }
            if (this == o) {
                return true;
            }
            if (o instanceof ValueInfo) {
                ValueInfo n = (ValueInfo) o;
                return this.key.equals(n.key);
            }
            return false;
        }

        public boolean isShow() {
            return isShow;
        }

        public void setShow(boolean show) {
            isShow = show;
        }
    }
}
